package com.zego.wawaji.ui.center.credit;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.log.PToast;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.base.observer.PObserver;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.base.CustomRecyclerView;
import com.zego.wawaji.ui.base.xlistview.ExListView;
import com.zego.wawaji.ui.bean.user.UserDetail;
import com.zego.wawaji.ui.center.credit.bean.Credit;
import com.zego.wawaji.ui.center.credit.bean.CreditConvert;
import com.zego.wawaji.ui.center.credit.bean.CreditList;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的积分
 * Created by Su on 2017/11/27.
 */
public class MyCreditAct extends BaseAppCompatActivity implements PObserver, View.OnClickListener, RequestComplete, ExListView.IXListViewListener {
    private CustomRecyclerView statusLayout;
    private ExListView exListView;
    private int page = 1; // 加载页码

    private CreditAdapter adapter;
    private List<Credit> list = new ArrayList<>();

    private TextView myPoint;  // 我的积分
    private RelativeLayout btnExchange;  // 兑换
    private UserDetail userDetail;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_credit_act);
        initView();
        onRefresh();
    }

    private void initView() {
        setTitle("我的积分");
        setBackView();
        MsgMgr.getInstance().attach(this);
        statusLayout = (CustomRecyclerView) findViewById(R.id.statusLayout);
        View mViewTop = LayoutInflater.from(this).inflate(R.layout.my_credit_top, null);
        myPoint = (TextView) mViewTop.findViewById(R.id.my_credit);
        btnExchange = (RelativeLayout) mViewTop.findViewById(R.id.btn_exchange);
        btnExchange.setOnClickListener(this);

        exListView = statusLayout.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(false);
        exListView.setPullLoadEnable(true);
        exListView.addHeaderView(mViewTop);
        adapter = new CreditAdapter(this, list);
        exListView.setAdapter(adapter);
        statusLayout.showLoading();
    }

    @Override
    public void onRefresh() {
        userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        myPoint.setText(String.valueOf(userDetail.getWwPoint()));
        exListView.setPullRefreshEnable(false);
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getWawaMgr().reqPointRecord(page, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getWawaMgr().reqPointRecord(page, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        exListView.stopRefresh();
        exListView.stopLoadMore();
        if (response.getUrlParam() == UrlParam.reqPointRecord) {
            setData(response);
        }
    }

    private void setData(HttpResponse response) {
        // 请求失败
        if (!response.isOk()) {
            if (list.size() != 0) {
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
                return;
            }
            statusLayout.showNoData("暂无数据", "重试", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    statusLayout.showLoading();
                    onRefresh();
                }
            });
            return;
        }

        // 非缓存
        if (!response.isCache()) {
            CreditList dataList = (CreditList) response.getBaseData();

            if (dataList.getCreditList().size() != 0) {
                //如果请求的第一页 应该把之前的数据都清掉，把返回的数据作为第一页
                if (page == 1) {
                    if (list.size() != 0) {
                        list.clear();
                    }
                    page = 1;
                }

                list.addAll(dataList.getCreditList());
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
            } else {
                if (page == 1) {
                    statusLayout.showNoData("暂无数据", "重试", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            statusLayout.showLoading();
                            onRefresh();
                        }
                    });
                } else {
                    exListView.setPullLoadEnable(false);
                }
            }
            return;
        }

        // 缓存
        if (page == 1) {
            CreditList dataList = (CreditList) response.getBaseData();

            if (dataList != null && dataList.getCreditList().size() != 0) {
                if (list.size() != 0) {
                    list.clear();
                }
                list.addAll(dataList.getCreditList());
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
            }
        }
    }

    /**
     * 刷新首页
     */
    public void refreshList() {
        onRefresh();
        if (adapter != null && adapter.getList() != null && adapter.getList().size() != 0) {
            exListView.setSelection(0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_exchange:  // 积分兑换娃娃币
                if (userDetail.getWwPoint() < 200) {
                    PToast.showCenterShort("积分最低达到200，才能兑换娃娃币哦！");
                    return;
                }

                ModuleMgr.getWawaMgr().reqExchangeWwcoin(userDetail.getWwPoint(), new RequestComplete() {
                    @Override
                    public void onRequestComplete(HttpResponse response) {
                        if (response.isOk()) {
                            CreditConvert creditConvert = (CreditConvert) response.getBaseData();
                            PickerDialogUtil.showWawaExchangeDialog(MyCreditAct.this, creditConvert.getWwCoinZs());
                            onRefresh();
                        }
                    }
                });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                userDetail = ModuleMgr.getCenterMgr().getMyInfo();
                myPoint.setText(String.valueOf(userDetail.getWwPoint()));
                break;
        }
    }
}
